package com.canxin.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.web.system.service.SystemService;


import com.canxin.entity.BinduserEntity;
import com.canxin.entity.ExamEntity;
import com.canxin.entity.NoticeEntity;
import com.canxin.entity.ScoreAnalyze;
import com.canxin.entity.ScoreEntity;
import com.canxin.entity.StudentsEntity;
import com.canxin.entity.UserFocusEntity;
import com.canxin.service.BinduserServiceI;
import com.canxin.service.ChatMenuServiceI;
import com.canxin.service.ExamServiceI;
import com.canxin.service.NoticeServiceI;
import com.canxin.service.ScoreAnalyzeServiceI;
import com.canxin.service.ScoreServiceI;
import com.canxin.service.StudentServiceI;
import com.canxin.service.impl.ExamServiceImpl;
import com.canxin.tools.CountRankCompareDesc;
import com.canxin.tools.DiffRankCompareDesc;
import com.canxin.tools.ListSortUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weixin.guanjia.message.entity.ReceiveText;
import weixin.guanjia.message.service.ReceiveTextServiceI;
import weixin.p3.oauth2.rule.RemoteWeixinMethod;

/**   
 * @Title: Controller
 * @Description: 用于处理聊天页面相关的后台逻辑
 * @author zhangdaihao
 * @date 2017-03-12 14:43:41
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/scoreAnalyzeController")
public class ScoreAnalyzeController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ScoreAnalyzeController.class);

	@Autowired
	private ChatMenuServiceI chatMenuService;
	@Autowired
	private ReceiveTextServiceI receiveTextService;	
	@Autowired
	private ExamServiceI examService;
	@Autowired
	private ScoreServiceI scoreService;
	@Autowired
	private StudentServiceI studentService;
	@Autowired
	private  ScoreAnalyzeServiceI scoreAnalyzeService;
	
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	@RequestMapping(params="getPerfectStudent")
	@ResponseBody
	public AjaxJson getPerfectStudent(HttpServletRequest request) throws IOException {
		AjaxJson j = new AjaxJson();
		ExamEntity currentExamEntity = examService.getAllExamsOrderByExamDate().get(0);
		ExamEntity lastExamEntity = examService.getAllExamsOrderByExamDate().get(1);
		
		List<StudentsEntity> studentList = studentService.getAllStudents();
		ArrayList studentArrayList = new ArrayList();
		for(StudentsEntity student:studentList)
		{
			String studentId = student.getId();
			ScoreEntity currentScoreEntity = scoreService.getScoresByStudentIdAndExamId(studentId, currentExamEntity.getExamid());
			ScoreEntity lastScoreEntity = scoreService.getScoresByStudentIdAndExamId(studentId, lastExamEntity.getExamid());
			if(currentScoreEntity==null || lastScoreEntity==null)
				continue;
			int diffRank = lastScoreEntity.getRank() - currentScoreEntity.getRank();
			ScoreAnalyze scoreAnalyze = new ScoreAnalyze(currentScoreEntity.getStudentid(),diffRank,currentScoreEntity,lastScoreEntity);
			studentArrayList.add(scoreAnalyze);
		}
        Collections.sort(studentArrayList, new DiffRankCompareDesc());
        JSONArray resultArray = new JSONArray();
        for(int i=0;i<10;i++)
        {
        	ScoreAnalyze scoreAnalyze = (ScoreAnalyze) studentArrayList.get(i);
        	Map resultMap  = scoreAnalyzeService.getWorstSubjectRank(scoreAnalyze.getCurrentScoreEntity());
        	JSONObject resultJson = new JSONObject();
        	resultJson.put("studentId", scoreAnalyze.getCurrentScoreEntity().getStudentid());
        	resultJson.put("currentRank", scoreAnalyze.getCurrentScoreEntity().getRank());
        	resultJson.put("lastRank", scoreAnalyze.getLastScoreEntity().getRank());
        	resultJson.put("studentName", scoreAnalyze.getCurrentScoreEntity().getStudentname());
        	resultJson.put("diffRank", scoreAnalyze.getDiffRank());
        	resultJson.put("subjectdiffRank", resultMap.get("diffRank"));
        	resultJson.put("subject", resultMap.get("subject"));
        	resultArray.add(resultJson);
        }
		j.setSuccess(true);
		j.setMsg(resultArray.toString());
		return j;
	}
	
	@RequestMapping(params="getWorstStudent")
	@ResponseBody
	public AjaxJson getWorstStudent(HttpServletRequest request) throws IOException {
		AjaxJson j = new AjaxJson();
		ExamEntity currentExamEntity = examService.getAllExamsOrderByExamDate().get(0);
		ExamEntity lastExamEntity = examService.getAllExamsOrderByExamDate().get(1);
		
		List<StudentsEntity> studentList = studentService.getAllStudents();
		ArrayList studentArrayList = new ArrayList();
		for(StudentsEntity student:studentList)
		{
			String studentId = student.getId();
			ScoreEntity currentScoreEntity = scoreService.getScoresByStudentIdAndExamId(studentId, currentExamEntity.getExamid());
			ScoreEntity lastScoreEntity = scoreService.getScoresByStudentIdAndExamId(studentId, lastExamEntity.getExamid());
			if(currentScoreEntity==null || lastScoreEntity==null)
				continue;
			int diffRank =  currentScoreEntity.getRank()-lastScoreEntity.getRank();
			ScoreAnalyze scoreAnalyze = new ScoreAnalyze(currentScoreEntity.getStudentid(),diffRank,currentScoreEntity,lastScoreEntity);
			studentArrayList.add(scoreAnalyze);
		}
        Collections.sort(studentArrayList, new DiffRankCompareDesc());
        JSONArray resultArray = new JSONArray();
        for(int i=0;i<10;i++)
        {
        	ScoreAnalyze scoreAnalyze = (ScoreAnalyze) studentArrayList.get(i);
        	Map resultMap  = scoreAnalyzeService.getWorstSubjectRank(scoreAnalyze.getCurrentScoreEntity());
        	JSONObject resultJson = new JSONObject();
        	resultJson.put("studentId", scoreAnalyze.getCurrentScoreEntity().getStudentid());
        	resultJson.put("currentRank", scoreAnalyze.getCurrentScoreEntity().getRank());
        	resultJson.put("lastRank", scoreAnalyze.getLastScoreEntity().getRank());
        	resultJson.put("studentName", scoreAnalyze.getCurrentScoreEntity().getStudentname());
        	resultJson.put("diffRank", scoreAnalyze.getDiffRank());
        	resultJson.put("subjectdiffRank", resultMap.get("diffRank"));
        	resultJson.put("subject", resultMap.get("subject"));
        	resultArray.add(resultJson);
        }
		j.setSuccess(true);
		j.setMsg(resultArray.toString());
		return j;
	}

	@RequestMapping(params="getAverageRank")
	@ResponseBody
	public AjaxJson getAverageRank(@RequestParam("num") String num) throws IOException {
		AjaxJson j = new AjaxJson();
		List<ExamEntity> examList = examService.getAllExamsOrderByExamDate().subList(0,Integer.parseInt(num)-1);
		List<StudentsEntity> studentList = studentService.getAllStudents();
		List resultList = new ArrayList();
		for(StudentsEntity student:studentList)
		{
			int avgRank = scoreService.getStudentAverageRankByStudentIdAndExamId(student.getId(),examList);
			Map resultMap = new HashMap();
			resultMap.put("studnentName",student.getChname());
			resultMap.put("avgRank",avgRank);
			resultList.add(resultMap);
		}
		j.setObj(resultList);
		return j;
	}

	@RequestMapping(params="getTotalRank")
	@ResponseBody
	public AjaxJson getTotalRank(HttpServletRequest request,@RequestParam("num") String num) throws IOException {
		AjaxJson j = new AjaxJson();
		ExamEntity currentExamEntity = examService.getAllExamsOrderByExamDate().get(0);
		List<ScoreEntity> scoreEntityList = scoreService.getStudentsCountBeyondParamsByExamId(currentExamEntity.getExamid(), num);
		ArrayList studentArrayList = new ArrayList();
		for(ScoreEntity scoreEntity:scoreEntityList)
		{
			ScoreEntity currentScoreEntity = scoreService.getScoresByStudentIdAndExamId(scoreEntity.getStudentid(), currentExamEntity.getExamid());
			int count = scoreService.getStudentRankCountBeyondParamsByStudentId(scoreEntity.getStudentid(), num);
			ScoreAnalyze scoreAnalyze = new ScoreAnalyze(scoreEntity.getStudentid(),count,currentScoreEntity);
			studentArrayList.add(scoreAnalyze);
		}
		  Collections.sort(studentArrayList, new CountRankCompareDesc());
	      JSONArray resultArray = new JSONArray();
	        for(int i=0;i<studentArrayList.size();i++)
	        {
	        	ScoreAnalyze scoreAnalyze = (ScoreAnalyze) studentArrayList.get(i);
	        	if(scoreAnalyze.getCurrentScoreEntity()==null)
	        		continue;
	        	Map resultMap  = scoreAnalyzeService.getWorstSubjectRank(scoreAnalyze.getCurrentScoreEntity());
	        	JSONObject resultJson = new JSONObject();
	        	resultJson.put("studentId", scoreAnalyze.getCurrentScoreEntity().getStudentid());
	        	resultJson.put("currentRank", scoreAnalyze.getCurrentScoreEntity().getRank());
	        	resultJson.put("studentName", scoreAnalyze.getCurrentScoreEntity().getStudentname());
	        	resultJson.put("count", scoreAnalyze.getCount());
	        	resultJson.put("subjectdiffRank", resultMap.get("diffRank"));
	        	resultJson.put("subject", resultMap.get("subject"));
	        	resultArray.add(resultJson);
	        }
			j.setSuccess(true);
			j.setMsg(resultArray.toString());
		
		return j;
	}
}
